﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _PLATFORM.DATA;

namespace _PLATFORM.WEB.Controllers
{ 
    public class SystemFittingTypeAssetController : Controller
    {
        public eCommPlatformRepoEntities db = new eCommPlatformRepoEntities();
        private Guid UserID = _PLATFORM.DATA.Custom_Classes.User.UserID;

        //
        // GET: /SystemFittingTypeAsset/

        public ViewResult Index()
        {
            var systemsAndFittingTypeAssets = new Dictionary<SYSTEM, List<FITTING_TYPE_ASSET>>();
            // Get My Systems
            var mySystems = db.SYSTEMs.Where(s => s.USER_ID == UserID).ToList();
            
            foreach (var sys in mySystems)
            {
                var systemFittingTypeAssets = db.SYSTEM_FITTING_TYPE_ASSET.Where(f => f.SYSTEM_ID == sys.SYSTEM_ID).ToList();
                
                if (systemFittingTypeAssets.Count > 0)
                {
                    var fittingTypeAssets = systemFittingTypeAssets.Select(t => db.FITTING_TYPE_ASSET.Where(f => f.FITTING_TYPE_ASSET_ID == t.FITTING_TYPE_ASSET_ID).ToList()[0]).ToList();
                    systemsAndFittingTypeAssets.Add(sys, fittingTypeAssets);
                }
            }

            ViewBag.SystemsAndFittingTypeAssets = systemsAndFittingTypeAssets;

            if (systemsAndFittingTypeAssets.Count == 0)
            {
                ViewBag.FITTING_TYPE_ASSET_ID = new SelectList(db.FITTING_TYPE_ASSET.Where(u => u.USER_ID == UserID), "FITTING_TYPE_ASSET_ID", "NAME");
                ViewBag.SYSTEM_ID = new SelectList(db.SYSTEMs, "SYSTEM_ID", "NAME");
                return View("Create");
            }
            else
            {
                return View(systemsAndFittingTypeAssets[mySystems[0]].ToList());
            }
        }

        //
        // GET: /SystemFittingTypeAsset/Details/5

        public ViewResult Details(decimal id)
        {
            SYSTEM_FITTING_TYPE_ASSET system_fitting_type_asset = db.SYSTEM_FITTING_TYPE_ASSET.Find(id);
            return View(system_fitting_type_asset);
        }

        //
        // GET: /SystemFittingTypeAsset/Create

        public ActionResult Create()
        {
            ViewBag.FITTING_TYPE_ASSET_ID = new SelectList(db.FITTING_TYPE_ASSET.Where(u => u.USER_ID == UserID), "FITTING_TYPE_ASSET_ID", "NAME");
            ViewBag.SYSTEM_ID = new SelectList(db.SYSTEMs, "SYSTEM_ID", "NAME");
            return View();
        } 

        //
        // POST: /SystemFittingTypeAsset/Create

        [HttpPost]
        public ActionResult Create(SYSTEM_FITTING_TYPE_ASSET system_fitting_type_asset, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                var fittingTypes = collection["FITTING_TYPE_ASSET_ID"].Split(',').ToList();

                foreach (string t in fittingTypes)
                {
                    system_fitting_type_asset.FITTING_TYPE_ASSET_ID = int.Parse(t);
                    db.SYSTEM_FITTING_TYPE_ASSET.Add(system_fitting_type_asset);
                    db.SaveChanges();
                }
                
                return RedirectToAction("Index");  
            }

            ViewBag.FITTING_TYPE_ASSET_ID = new SelectList(db.FITTING_TYPE_ASSET, "FITTING_TYPE_ASSET_ID", "NAME", system_fitting_type_asset.FITTING_TYPE_ASSET_ID);
            ViewBag.SYSTEM_ID = new SelectList(db.SYSTEMs, "SYSTEM_ID", "NAME", system_fitting_type_asset.SYSTEM_ID);
            return View(system_fitting_type_asset);
        }
        
        //
        // GET: /SystemFittingTypeAsset/Edit/5
 
        public ActionResult Edit(decimal id)
        {
            SYSTEM_FITTING_TYPE_ASSET system_fitting_type_asset = db.SYSTEM_FITTING_TYPE_ASSET.Find(id);
            ViewBag.FITTING_TYPE_ASSET_ID = new SelectList(db.FITTING_TYPE_ASSET, "FITTING_TYPE_ASSET_ID", "NAME", system_fitting_type_asset.FITTING_TYPE_ASSET_ID);
            ViewBag.SYSTEM_ID = new SelectList(db.SYSTEMs, "SYSTEM_ID", "NAME", system_fitting_type_asset.SYSTEM_ID);
            return View(system_fitting_type_asset);
        }

        //
        // POST: /SystemFittingTypeAsset/Edit/5

        [HttpPost]
        public ActionResult Edit(SYSTEM_FITTING_TYPE_ASSET system_fitting_type_asset)
        {
            if (ModelState.IsValid)
            {
                db.Entry(system_fitting_type_asset).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.FITTING_TYPE_ASSET_ID = new SelectList(db.FITTING_TYPE_ASSET, "FITTING_TYPE_ASSET_ID", "NAME", system_fitting_type_asset.FITTING_TYPE_ASSET_ID);
            ViewBag.SYSTEM_ID = new SelectList(db.SYSTEMs, "SYSTEM_ID", "NAME", system_fitting_type_asset.SYSTEM_ID);
            return View(system_fitting_type_asset);
        }

        //
        // GET: /SystemFittingTypeAsset/Delete/5
 
        public ActionResult Delete(decimal id)
        {
            SYSTEM_FITTING_TYPE_ASSET system_fitting_type_asset = db.SYSTEM_FITTING_TYPE_ASSET.Find(id);
            return View(system_fitting_type_asset);
        }

        //
        // POST: /SystemFittingTypeAsset/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(decimal id)
        {            
            SYSTEM_FITTING_TYPE_ASSET system_fitting_type_asset = db.SYSTEM_FITTING_TYPE_ASSET.Find(id);
            db.SYSTEM_FITTING_TYPE_ASSET.Remove(system_fitting_type_asset);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}